package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.mapper.CheckItemMapper;
import com.xr.pojo.CheckItem;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    //本地调用即可
    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<CheckItem> page=checkItemMapper.selectByFindString(queryPageBean.getQueryString());


        return new PageResult(page.getTotal(),page.getResult());
    }

    //检查项列表实现
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.selectByExample(null);
    }

    @Override
    public Integer AddCheckItem(CheckItem checkItem) {
        return checkItemMapper.insertSelective(checkItem);
    }

    @Override
    public Integer DelCheckItem(Integer id) throws CustomException {
        //先判断这个检查项是否被检查组使用了
        Integer count = checkItemMapper.isUseInCheckGroup(id);
        if (count>0){
            //被使用了则不能删除,手动抛出异常
            throw new CustomException(MessageConstant.CHECKITEM_IN_USE);
        }
        return checkItemMapper.deleteByPrimaryKey(id);

    }

    @Override
    public Integer UpdCheckItem(CheckItem checkItem) {
        return checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }
}
